/* 
 * File:   MultiSum.h
 * Author: lusitanio
 *
 * Created on 11 de Abril de 2012, 1:01
 */

#ifndef MULTISUM_H
#define	MULTISUM_H
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
using namespace std;

class MultiSum : public AppData {
private:
    // AppData variables
    int size_;
    char* str_;



public:
    //Specific
    map<int, Histograma> dados;

    MultiSum() : AppData(MSCOOP_DATA), size_(0), str_(NULL) {
    }

    MultiSum(int s, Histograma h) : AppData(MSCOOP_DATA), size_(0), str_(NULL) {
        insertSum(s, h);
    }

    MultiSum(const MultiSum& data) : AppData(MSCOOP_DATA) {
        dados = data.dados;
        size_ = data.size_;
        if (size_ > 0) {
            str_ = new char[size_];
            strcpy(str_, data.str_);
        } else str_ = NULL;
    }

    void insertSum(int sensor, Histograma hist) {
        dados.insert(make_pair(sensor, hist));
    }

    Histograma getSum(int sensor) {
        return dados.at(sensor);
    }

    void insertMultiSum(MultiSum ms) {
        map<int, Histograma>::iterator it;
        for (it = ms.dados.begin(); it != ms.dados.end(); it++) {
            int sensor = (*it).first;
            Histograma h = (*it).second;
            insertSum(sensor,h);
        }
        
    }

    virtual ~MultiSum() {
        if (str_ != NULL) delete []str_;

    }

    char* str() {
        return str_;
    }

    virtual int size() const {
        return AppData::size() + size_;
    }

    void set_string(const char* s) {
        if ((s == NULL) || (*s == 0))
            str_ = NULL, size_ = 0;
        else {
            size_ = strlen(s) + 1;
            str_ = new char[size_];
            assert(str_ != NULL);
            strcpy(str_, s);
        }
    }

    virtual AppData* copy() {
        return new MultiSum(*this);
    }
    
    void print(){
        map<int, Histograma>::iterator it;
        puts("***** MULTISUM *****");
        printf("***** TAMANHO: %d\n",dados.size());
        puts("*****  CONTEM  *****");
        for (it = dados.begin(); it != dados.end(); it++) {
            int sensor = (*it).first;
            printf("Sumario do sensor: %d\n",sensor);
        }        
        puts("*****  FINAL   *****");
    }
    
    const char* print_sums(){
        stringstream w_return(stringstream::out);
        map<int, Histograma>::iterator it;
        w_return << "|";
        for (it = dados.begin(); it != dados.end(); it++) {
            int sensor = (*it).first;
            w_return << sensor << " ";
        }           
        w_return << "|";
        return w_return.str().c_str();
    }
};


#endif	/* MULTISUM_H */

